#include "QCommonEx.h"
#include <QtWidgets>
#include <QMessageBox>

QCommonEx::QCommonEx(QObject *parent) :
    QObject(parent)
{
    qucikView = NULL;
}

void QCommonEx::onClicked()
{
    if (qucikView == NULL)
        return;

    QString username;
    QString password;
    QQuickItem* root = qucikView->rootObject();
    QQuickItem* accountEdit = root->findChild<QQuickItem*>("accountEdit");
    if (accountEdit)
    {
        QVariant returnVar = accountEdit->property("text");
        username = returnVar.toString();
    }

    QQuickItem* passwordEdit = root->findChild<QQuickItem*>("passwordEdit");
    if (passwordEdit)
    {
        QVariant returnVar = passwordEdit->property("text");
        password = returnVar.toString();
    }


    QQuickItem* login = root->findChild<QQuickItem*>("login");
    //QList<QQuickItem *>	a = root->childItems();
    //qDebug() << a;
    if (login)
    {
        if (password == "" && username == "abc")
        {

            QQuickItem* rectLogin = root->findChild<QQuickItem*>("rectLogin");
            QQuickItem* rectMain = root->findChild<QQuickItem*>("rectMain");
            if (rectMain != NULL && rectLogin != NULL)
            {
                rectLogin->setProperty("visible", false);
                rectMain->setProperty("visible", true);
            }


            // root->setProperty("color", "red");
        }
        else
        {
            QString msg = QString("account:%1\r\npassword:%2").arg(username).arg(password);
            QMetaObject::invokeMethod(login, "showMessageBox",
                                      Q_ARG(QVariant, msg));
            //login->setProperty("msgBox.text", returnVar);
            //login->setProperty("msgBox.visable", QVariant(true));
        }
    }


    //QMessageBox::warning(0, "ad", "adf");
}

